#include "GhostLevel.h"
#include "../../../GameConfig.h"
#include "../../../Framework/SceneManager.h"
#include "../../../Scenes/Level/LevelManager.h"
#include "../../../Player/PlayerInfo.h"
#include "../../../Support/GameSupport.h"
#include "../../../Data/GameData.h"
#include "../../../Framework/SoundManager.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "Cocos2dxStore.h"
using namespace soomla;
#endif


#define kDistanceStar 10

GhostLevel::GhostLevel()
{
    //GhostObject::GhostObject();
}

void GhostLevel::initalize(EGhostColor color, EGhostEyeColor eyeColor, Vec2 position, float scale, int id)
{
	ID = id;
    GhostObject::initalize(color,EGhostFaceState::kGhostFaceState_None,EGhostHandState::kGhostHandState_None,eyeColor, position, scale, false);
	//GhostObject::initalize(color, position, scale);

    

	//initStar();
    m_body->setTitleFontSize(130);
    
    m_body->setTitleColor(GameSupport::getGhostColor(color));

    if(PlayerInfo::getInstance()->level->getLevelEnable(LevelManager::s_mode, ID) && PlayerInfo::getInstance()->level->getLevelStar(LevelManager::s_mode,ID) != -1)
        initComponent();
    else
    {
        
        m_body->setTitleText(String::createWithFormat("%d", id)->getCString());
        m_body->setTitleFontName(kFont_Comica_BD_Bold);
        
        
        
        
       
    }
        m_body->addClickEventListener(CC_CALLBACK_1(GhostLevel::tapGhost, this));
	//setState(EGhostState::kGhostState_Default);
    //auto func = CallFuncN::create(CC_CALLBACK_0(GhostObject::setRandomFace,this));
    
}
void GhostLevel::initComponent()
{
//    switch (m_numStar) {
//            
//        case 0:
//            setFace(EGhostFaceState::kGhostFaceState_Free, EGhostStatus::kGhostStatus_Sad);
//            break;
//        case 1:
//            setFace(EGhostFaceState::kGhostFaceState_Free, EGhostStatus::kGhostStatus_Idle);
//            break;
//        case 2:
//            setFace(EGhostFaceState::kGhostFaceState_Free,EGhostStatus::kGhostStatus_Happy);
//            break;
//        case 3:
//            setFace(EGhostFaceState::kGhostFaceState_Free, EGhostStatus::kGhostStatus_Lol);
//            break;
//        default:
//            break;
//    }
    
    
    Label* lblLevel = Label::create(String::createWithFormat("%d",ID)->getCString(), kFont_Comica_BD_Bold, m_body->getTitleFontSize());
    
    //lblLevel->setFontName(kFont_Comica_BD_Bold);
    lblLevel->setColor(m_body->getTitleColor());
    lblLevel->setPosition(Vec2(-m_body->getContentSize().width/10
                               ,m_body->getContentSize().height/2));
    m_body->addChild(lblLevel);
}
void GhostLevel::initStar()
{
    m_numStar = PlayerInfo::getInstance()->level->getLevelStar(LevelManager::s_mode, ID);
    
    switch (m_numStar) {
        case -1:
            m_starLeft = Sprite::create("star_white.png");
            m_starRight = Sprite::create("star_white.png");
            m_starTop = Sprite::create("star_white.png");
            
            break;
        case 0:
            m_starLeft = Sprite::create("star_white.png");
            m_starRight = Sprite::create("star_white.png");
            m_starTop = Sprite::create("star_white.png");

            break;
        case 1:
            m_starLeft = Sprite::create("star_yellow.png");
            m_starRight = Sprite::create("star_white.png");
            m_starTop = Sprite::create("star_white.png");
            break;
        case 2:
            m_starLeft = Sprite::create("star_yellow.png");
            m_starRight = Sprite::create("star_yellow.png");
            m_starTop = Sprite::create("star_white.png");
            break;
        case 3:
            m_starLeft = Sprite::create("star_yellow.png");
            m_starRight = Sprite::create("star_yellow.png");
            m_starTop = Sprite::create("star_yellow.png");
            break;
        default:
            break;
    }
	

	m_starLeft->setScale(m_scale);
	m_starRight->setScale(m_scale);
	m_starTop->setScale(m_scale);

	float newWidthGhost = m_body->getContentSize().width*m_scale;
	float newHeightGhost = m_body->getContentSize().height*m_scale;
	m_starLeft->setPosition(m_body->getPosition().x - newWidthGhost / 2 , m_body->getPosition().y + newHeightGhost / 3);
	m_starRight->setPosition(m_body->getPosition().x + newWidthGhost / 2 , m_body->getPosition().y + newHeightGhost / 3);
	m_starTop->setPosition(m_body->getPosition().x, m_body->getPosition().y + newHeightGhost / 2);

	this->addChild(m_starLeft);
	this->addChild(m_starRight);
	this->addChild(m_starTop);

}

void GhostLevel::tapGhost(cocos2d::Ref *pSender)
{
	if (!LevelManager::s_isActive)
	{
		if (!PlayerInfo::getInstance()->level->getLevelEnable(LevelManager::s_mode, ID))
			return;

		if (!PlayerInfo::getInstance()->iap->getModeFullVersion(LevelManager::s_mode) &&
			ID >  GameData::getInstance()->config.getMaxLevelFree(LevelManager::s_mode))
		{
			//open IAP
			char s[100];
			switch(LevelManager::s_mode)
			{
			case EGameMode::kMode_Easy: sprintf(s, "%s", "iap_easy"); break;
			case EGameMode::kMode_Normal: sprintf(s, "%s", "iap_normal"); break;
			case EGameMode::kMode_Hard: sprintf(s, "%s", "iap_hard"); break;
			}

			#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
				CCError *soomlaError = NULL;
				CCStoreInventory::sharedStoreInventory()->buyItem(s, &soomlaError);
				if (soomlaError)
				{
				CCSoomlaUtils::logException("Error IAP on menu scene:", soomlaError);
				return;
				}

				PlayerInfo::getInstance()->iap->setModeFullVersion(EGameMode::kMode_Hard, true);
			#endif

				switch(LevelManager::s_mode)
				{
				case EGameMode::kMode_Easy: PlayerInfo::getInstance()->iap->setModeFullVersion(EGameMode::kMode_Easy, true); break;
				case EGameMode::kMode_Normal: PlayerInfo::getInstance()->iap->setModeFullVersion(EGameMode::kMode_Normal, true); break;
				case EGameMode::kMode_Hard: PlayerInfo::getInstance()->iap->setModeFullVersion(EGameMode::kMode_Hard, true); break;
				}

			return;
		}

		LevelManager::s_isActive = true;
		LevelManager::s_ID = ID;
        
        //this->changeGhostState(EGhostStatus::kGhostStatus_Yeah);
        
        SoundManager::getInstance()->playSound(ESound::kSound_Click);
        SceneManager::getInstance()->changeScene(this->getParent()->getParent()->getParent(), EScene::kScene_Play);
	}
}

